package org.dtrd.modules.evaluation.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.dtrd.base.BaseController;
import org.dtrd.common.api.vo.Result;
import org.dtrd.modules.base.service.BaseCommonService;
import org.dtrd.modules.evaluation.constant.Evaluation;
import org.dtrd.modules.evaluation.entity.bean.JobInfo;
import org.dtrd.modules.evaluation.entity.po.DtrdEntEvaluationRdJob;
import org.dtrd.modules.evaluation.service.IDtrdEntEvaluationRdJobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.nio.charset.MalformedInputException;
import java.util.List;
import java.util.Optional;

import static org.dtrd.common.constant.CommonConstant.OPERATE_TYPE_ADD;
import static org.dtrd.common.constant.CommonConstant.OPERATE_TYPE_QUERY;
import static org.dtrd.config.system.SystemState.ResponseState.*;

/**
 * @Author QR
 * @Date 2021/11/1 10:36 AM
 */
@Api(tags = "量表评估")
@RestController
public class JobController extends BaseController {

    @Autowired
    private IDtrdEntEvaluationRdJobService jobService;
    @Autowired
    private BaseCommonService baseCommonService;

    @ApiOperation("添加马氏工作倦怠评估")
    @PostMapping(value = {"/105/43/1054301", "/100/17/1001713"})
    public Result<?> saveJobInfo(@RequestBody @Validated JobInfo jobInfo) {
        // 非空校验
        List<Integer> input = jobInfo.getInput();
//        if (input == null || input.isEmpty() || input.contains(null)) {
//            return error(STATE_PARAMS_MISSING);
//        }
//        // 校验量表题目数量
//        boolean inValidInput = input.size() != Evaluation.ProblemNum.JOB.getNum();
//        if (inValidInput) {
//            return error(STATE_PARAMS_ERROR);
//        }
        // 判断是否有 -1 或者 超出分值氛围 的选项, vue 的数字列表选项如果为空会默认为 -1
//        Optional<Integer> any = input.stream().filter(answer -> answer < 0 || answer > 8).findAny();
//        if (any.isPresent()) {
//            return error(STATE_PARAMS_ERROR);
//        }
        JobInfo info = jobService.saveJobInfo(jobInfo);
        String logContent = String.format("添加马氏工作倦怠评估，添加用户id：%d", jobInfo.getPatientId());
        baseCommonService.addLog(logContent, OPERATE_TYPE_ADD);
        return Result.OK(info);
    }

    @ApiOperation("查询最新的马氏工作倦怠评估")
    @GetMapping("/105/43/1054302")
    public Result<?> getJobInfoLatest(@PathParam("patientId") Integer patientId) {
        if (isIntParamInvalid(patientId)) {
            return error(STATE_PARAMS_MISSING);
        }
        JobInfo jobInfo = jobService.getJobInfoLatest(patientId);
        String logContent = String.format("查询最新的马氏工作倦怠评估，查询id：%d", patientId);
        baseCommonService.addLog(logContent, OPERATE_TYPE_QUERY);
        return Result.OK(jobInfo);
    }

    @ApiOperation("查询最新的马氏工作倦怠评估列表")
    @GetMapping("/105/43/1054303")
    public Result<?> getJobList(
            @ApiParam("患者Id") @RequestParam("patientId") Integer patientId
    ) {
        List<DtrdEntEvaluationRdJob> list = jobService.getJopEvaluationList(patientId);
        return CollectionUtil.isNotEmpty(list) ? Result.OK(list.stream().map(po -> new JobInfo().parseFromPo(po))) : error(STATE_NO_RECORD);
    }
}
